package com.gxa.service.employee.impl;

import com.gxa.common.R.ResponseData;
import com.gxa.mapper.employee.WorkEmployeeListMapper;
import com.gxa.pojo.Employee;
import com.gxa.service.employee.WorkEmployeeService;
import com.gxa.utils.EmployeeAge;
import com.gxa.utils.EmployeeGrade;
import com.gxa.utils.WorkAge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkEmployeeServiceImpl implements WorkEmployeeService {

    @Autowired(required = false)
    private WorkEmployeeListMapper workEmployeeListMapper;
    /**
     * 分页模糊查询所有在职员工信息
     * @param currentPage   当前页数
     * @param pageSize      每页显示条数
     * @param findKeyword   姓名关键字，如果无则为空
     * @return              返回员工信息总条数
     */
    @Override
    public ResponseData selectAllWorkEmployeeList(String currentPage, String pageSize, String findKeyword) {
        ResponseData responseData = new ResponseData();
        //在职员工总条数
        Integer count = workEmployeeListMapper.selectAllCount(findKeyword);

        int currentPage_int = Integer.parseInt(currentPage); //当前页
        int pageSize_int = Integer.parseInt(pageSize);       //每页显示条数
        int allPage = (count - 1)/pageSize_int + 1;          //总的页数
        int startIndex = (currentPage_int - 1) * pageSize_int;   //每页从第几条开始查询

        //分页模糊查询所有在职员工信息
        List<Employee> employeeList = workEmployeeListMapper.selectWorkEmployeeForPageAndEname(findKeyword, startIndex, pageSize_int);
        for (Employee employee: employeeList) {
            Date birthday = employee.getBirthday();           //获取员工出生日期
            Date entryDate = employee.getEntryDate();         //获取员工入职日期

            int age = EmployeeAge.getEmployeeAge(birthday);   //EmployeeAge计算员工年龄工具类计算员工年龄
            int gongLin = WorkAge.getWorkAge(entryDate);      //WorkAge计算员工工龄工具类调用getWorkAge方法计算员工工龄
            String grade = EmployeeGrade.getGrade(gongLin);   //EmployeeGrade工具类通过员工gongLin计算员工级别
            employee.setAge(age);
            employee.setGongLin(gongLin);
            employee.setGrade(grade);
        }
        responseData.setCode(200);
        responseData.setMsg("查询全部在职员工信息成功");
        responseData.setTotal(count);
        responseData.setData(employeeList);
        return responseData;
    }
}
